package com.ruoyi.common.utils;

import java.util.Random;

public class RandomCharData {

    //可以将字符转换赋值给int类型，查看其ASCII码
    public static void main(String[] args) {
        genNum(20);

        genNumChar(20);
        genNumChar1(20);


    }

    private static void genNum(int len) {
        //随机生成纯数字
        for (int i = 0; i < 3; i++) createData(len);
        System.out.println("---------------");
    }

    private static void genNumChar(int len) {
        //生成数字字母
        for (int i = 0; i < 3; i++)
            createRandomCharData(len);
        System.out.println("---------------");
    }

    private static void genNumChar1(int len) {
        //生成数字字母符号
        for (int i = 0; i < 3; i++)
            createRandomCharSymbolData(len);
        System.out.println("---------------");
    }

    //根据指定长度生成字母和数字的随机数
    //0~9的ASCII为48~57
    //A~Z的ASCII为65~90
    //a~z的ASCII为97~122
    public static void createRandomCharData(int length) {
        String result = createRandomCharPass(length);
        System.out.println(length + " random data: " +result);
    }

    public static String createRandomCharPass(int length) {
        StringBuilder sb = new StringBuilder();
        Random rand = new Random();//随机用以下三个随机生成器
        Random randdata = new Random();
        int data = 0;
        for (int i = 0; i < length; i++) {
            int index = rand.nextInt(3);
            //目的是随机选择生成数字，大小写字母
            switch (index) {
                case 0:
                    data = randdata.nextInt(10);//仅仅会生成0~9
                    sb.append(data);
                    break;
                case 1:
                    data = randdata.nextInt(26) + 65;//保证只会产生65~90之间的整数
                    sb.append((char) data);
                    break;
                case 2:
                    data = randdata.nextInt(26) + 97;//保证只会产生97~122之间的整数
                    sb.append((char) data);
                    break;
            }
        }
        return sb.toString();
    }

    //根据指定长度生成字母和数字的随机数
    //0~9的ASCII为48~57
    //A~Z的ASCII为65~90
    //a~z的ASCII为97~122
    public static void createRandomCharSymbolData(int length) {
        String result = createRandomCharSymbolPass(length);
        System.out.println(length + " random data: " + result);
    }

    public static String createRandomCharSymbolPass(int length) {
        StringBuilder sb = new StringBuilder();
        Random rand = new Random();//随机用以下三个随机生成器
        Random randdata = new Random();
        int data = 0;
        for (int i = 0; i < length; i++) {
            int index = rand.nextInt(4);
            //目的是随机选择生成数字，大小写字母
            switch (index) {
                case 0:
                    data = randdata.nextInt(10);//仅仅会生成0~9
                    sb.append(data);
                    break;
                case 1:
                    data = randdata.nextInt(26) + 65;//保证只会产生65~90之间的整数
                    sb.append((char) data);
                    break;
                case 2:
                    data = randdata.nextInt(26) + 97;//保证只会产生97~122之间的整数
                    sb.append((char) data);
                    break;
                case 3:
                    data = randdata.nextInt(15) + 32;//保证只会产生97~122之间的整数
                    sb.append((char) data);
                    break;
            }
        }
        return sb.toString();
    }

    //根据指定长度生成纯数字的随机数
    public static void createData(int length) {
        String data = createRandomNumPass(length);
        System.out.println(length + " random data: " + data);
    }

    public static String createRandomNumPass(int length) {
        StringBuilder sb = new StringBuilder();
        Random rand = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(rand.nextInt(10));
        }
        return sb.toString();
    }
}